#!/bin/bash
##打包日志文件
dir="$1"
if [[ -z "$2" ]];then
	save_dir="/usr/local/log/"
else
	save_dir="$2"
fi
mkdir -p $save_dir
if [ -z $dir ];then
	dir='/usr/local/bigdataserver/bigdata-sixone/logs/sixone/host/'
else
	if [ $dir == '-h' -o $dir == '--help' ];then
		echo -e "使用语法: bash \${shell_file} \${日志路径} \${日志文件临时存放路径} \${是否使用半自动化: answer}\n例如半自动化: bash logs.sh /usr/log/ /save/ answer\n完全自动化:  bash logs.sh /usr/log/ /save/"
		exit 11
	fi
fi
if [[ "$3" == 'answer' ]];then
	echo -e "需要备份的日志所在目录: $dir\n需要存放备份日志压缩包的目录: $save_dir \n按回车确认[将删除原日志目录]"
	read dd
fi
mkdir -p $save_dir
if [[ -d "$dir" ]];then
	if [[ -z `ls $dir` ]];then
		echo '该目录不存在文件'
		exit 11
	else
		echo '正在整理'
		time=`date +%Y-%m-%d | tr "/" "-"`
		#file_list=`ls -l --time-style=full | awk '{print $6,$9}' | grep $time | awk '{print $2}' `
		#for i in $file_list;do
		#	cp -i $dir/$i ${save_dir}
		#done
		zip -r ${time}.zip ${dir}*
		if [[ "$?" -eq "0" ]];then
			if [[ -f "${time}.zip" ]];then
				mv  ${time}.zip ${save_dir}
				echo -e "文件打包成功,已存放至\n\n\t${save_dir}${time}.zip\n"
				rm -rf ${dir}*
				echo "[$dir]目录数据已清除"
			else
				echo -e "文件: \t${save_dir}${time}.zip\t已存在,本次保存取消"
			fi
		else
			echo '文件打包失败'
		fi
	fi
else
	echo "目录[$dir]不存在"
fi

